<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:ed="livcos.org/ns/editor"
  xmlns:e="livcos.org/ns/editor"
  xmlns:t="livcos.org/ns/cosmos/type"
  xmlns:f="livcos.org/ns/xslt/function"
  exclude-result-prefixes="ed e t f doc">
	
	<xsl:import href="../../common/base.xsl"/>
	

	<xsl:template match="e:info">
		<xsl:param name="node"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<xsl:value-of select="f:eval($node, @path, .)"/>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:template">
		<xsl:param name="node"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:if test="not(@label)"><xsl:value-of select="*/@label[1]"/></xsl:if>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<xsl:apply-templates select="*" mode="tmpl">
					<xsl:with-param name="node" select="$node"/>
				</xsl:apply-templates>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:text | e:integer">
		<xsl:param name="node"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<xsl:apply-templates select="." mode="tmpl">
					<xsl:with-param name="node" select="$node"/>
				</xsl:apply-templates>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:label" mode="tmpl">
		<xsl:sequence select="node()"/>
	</xsl:template>
	
	<xsl:template match="e:text | e:integer" mode="tmpl">
		<xsl:param name="node"/>
		<xsl:if test="parent::e:template and @label">
			<xsl:text> </xsl:text>
			<xsl:value-of select="@label"/>
			<xsl:text>: </xsl:text>
		</xsl:if>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
		<input name="content_{$inputCount}" value="{f:eval($node, @path, .)}" type="text">
			<xsl:if test="@onChange">
				<xsl:attribute name="onkeyup">
					<xsl:text>this.form._panel.ed_</xsl:text>
					<xsl:value-of select="@onChange"/>
					<xsl:text>(this, event, '</xsl:text>
					<xsl:value-of select="@path"/>
					<xsl:text>')</xsl:text>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="not((@path = '.') or contains(@path,'title') or contains(@path,'label'))">
				<xsl:attribute name="autocomplete">off</xsl:attribute>
			</xsl:if>
			<xsl:if test="@length">
				<xsl:attribute name="size" select="@length"/>
			</xsl:if>
			<xsl:if test="not(@length)">
				<xsl:attribute name="style">width: 100%</xsl:attribute>
			</xsl:if>
			<xsl:if test="local-name() = 'integer'">
				<xsl:attribute name="style">text-align: right</xsl:attribute>
			</xsl:if>
			<xsl:if test="@comment">
				<xsl:attribute name="title" select="@comment"/>
			</xsl:if>
		</input>
		<xsl:if test="@unit">
			<span style="font-size: 80%"><xsl:value-of select="@unit"/></span>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="e:text[@multiline = '1']">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<xsl:if test="@label">
				<td class="ed_label">
					<xsl:value-of select="@label"/>
					<xsl:text>:</xsl:text>
				</td>
			</xsl:if>
			<td class="ed_entry">
				<xsl:if test="not(@label)">
					<xsl:attribute name="colspan">2</xsl:attribute>
				</xsl:if>
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<xsl:choose>
					<xsl:when test="@plain = 1">
						<input name="type_{$inputCount}" value="plain" type="hidden"/>
						<div style="overflow: auto;">
							<textarea class="input" name="content_{$inputCount}">
								<xsl:apply-templates select="f:eval($node, @path, .)" mode="root_escape"/>
							</textarea>
						</div>
					</xsl:when>
					<xsl:otherwise>
						<div class="richtext_input" name="content_{$inputCount}">
							<xsl:apply-templates select="f:eval($node, @path, .)" mode="root_content"/>
						</div>
					</xsl:otherwise>
				</xsl:choose>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:choice">
		<xsl:param name="node"/>
		<!-- TODO 9: exclusive switch... -->
		<xsl:apply-templates select="e:option/*">
			<xsl:with-param name="node" select="$node"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="e:ref | e:url | e:user">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<div class="ref_input">
					<xsl:variable name="val" select="if (@text) then f:eval($node, @text, .) else f:eval($node, @path, .)"/>
					<input name="content_{$inputCount}" value="{$val}" type="text" autocomplete="off"/>
				</div>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:time">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<table cellspacing="0" cellpadding="0">
					<tr>
						<td>
							<!-- overflow:auto div to work around input cursor bug in Firefox -->
							<div>
								<input name="content_{$inputCount}" value="{f:eval($node, @path, .)}" type="text"
									onfocus="this.parentNode.style.overflow = 'auto'" onblur="this.parentNode.style.overflow = 'visible'"
								/>
							</div>
						</td>
						<td>
							<input type="button" value="now">
								<xsl:attribute name="onclick">
									<xsl:text>this.form.content_</xsl:text>
									<xsl:value-of select="$inputCount"/>
									<xsl:text>.value = (new Date()).format('HH:mm:ss');</xsl:text>
								</xsl:attribute>
							</input>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:date | e:dateTime">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<div class="{local-name()}_input">
					<input name="content_{$inputCount}" value="{f:eval($node, @path, .)}" type="text"/>
				</div>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:percentage">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<select name="content_{$inputCount}">
					<option value="">-</option>
					<xsl:variable name="currentValue" select="f:eval($node, @path, .)"/>
					<xsl:for-each select="0 to 10">
						<xsl:variable name="val" select=". * 10"/>
						<option value="{$val}">
							<xsl:if test="($currentValue != '') and ($currentValue = $val)">
								<xsl:attribute name="selected">true</xsl:attribute>
							</xsl:if>
							<xsl:value-of select="$val"/>
							<xsl:text>%</xsl:text>
						</option>
					</xsl:for-each>
				</select>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:priority">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<select name="content_{$inputCount}">
					<xsl:variable name="currentValue" select="f:eval($node, @path, .)"/>
					<xsl:for-each select="1 to 5">
						<option value="{.}">
							<xsl:if
								test="(($currentValue != '') and ($currentValue = .)) or (($currentValue = '') and (. = 3))">
								<xsl:attribute name="selected">true</xsl:attribute>
							</xsl:if>
							<xsl:value-of select="."/>
							<xsl:choose>
								<xsl:when test=". = 1"> (critical)</xsl:when>
								<xsl:when test=". = 2"> (high)</xsl:when>
								<xsl:when test=". = 3"> (normal)</xsl:when>
								<xsl:when test=". = 4"> (low)</xsl:when>
								<xsl:when test=". = 5"> (none)</xsl:when>
							</xsl:choose>
						</option>
					</xsl:for-each>
				</select>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:enum">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<select name="content_{$inputCount}">
					<xsl:variable name="currentValue" select="f:eval($node, @path, .)"/>
					<xsl:for-each select="e:option">
						<option value="{@value}">
							<xsl:if test="($currentValue != '') and ($currentValue = @value)">
								<xsl:attribute name="selected">true</xsl:attribute>
							</xsl:if>
							<xsl:value-of select="."/>
						</option>
					</xsl:for-each>
				</select>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="e:boolean">
		<xsl:param name="node"/>
		<xsl:variable name="inputCount" select="count(preceding::*) + 1"/>
		<tr>
			<td class="ed_label">
				<xsl:value-of select="@label"/>
				<xsl:text>:</xsl:text>
			</td>
			<td class="ed_entry">
				<input name="path_{$inputCount}" value="{@path}" type="hidden"/>
				<xsl:variable name="val" select="f:eval($node, @path, .)"/>
				<input id="content_{$inputCount}" name="content_{$inputCount}" value="{$val}" type="hidden"/>
				<input type="checkbox" style="width: auto"
					onclick="document.getElementById('content_{$inputCount}').value = (this.checked)?'1':'0'">
					<xsl:if test="$val = '1'">
						<xsl:attribute name="checked">true</xsl:attribute>
					</xsl:if>
				</input>
			</td>
		</tr>
	</xsl:template>
	
	
	<xsl:template match="node()" mode="content root_content" priority="0">
		<xsl:copy>
			<xsl:apply-templates select="node()" mode="content"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="comment()" mode="content root_content" priority="0.1">
		<xsl:value-of select="."/>
	</xsl:template>
	
	<xsl:template match="*" mode="root_content" priority="0.1">
		<xsl:apply-templates select="node()" mode="content"/>
	</xsl:template>
	
	<xsl:template match="*" mode="content" priority="0.1">
		<xsl:variable name="htmlTag">
			<xsl:if test="namespace-uri(.) = 'livcos.org/ns/doc'">
				<xsl:choose>
					<xsl:when test="local-name(.) = 'br'">br</xsl:when>
					<xsl:when test="local-name(.) = 'strong'">b</xsl:when>
					<xsl:when test="local-name(.) = 'em'">i</xsl:when>
					<xsl:when test="local-name(.) = 'u'">u</xsl:when>
					<xsl:when test="local-name(.) = 's'">strike</xsl:when>
					<xsl:when test="local-name(.) = 'sub'">sub</xsl:when>
					<xsl:when test="local-name(.) = 'sup'">sup</xsl:when>
				</xsl:choose>
			</xsl:if>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$htmlTag != ''">
				<xsl:element name="{$htmlTag}" namespace="http://www.w3.org/1999/xhtml">
					<xsl:apply-templates select="node()" mode="content"/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<pre style="color: red"><xsl:apply-templates select="." mode="escape"/></pre>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="*[(local-name(.) = 'link') and (namespace-uri(.) = 'livcos.org/ns/doc')]" mode="content">
		<a id="{@id}">
			<xsl:attribute name="href">
				<!-- href attribute is not important for the editor panel content,
					since the link will be matched via it's ID -->
				<xsl:choose>
					<xsl:when test="normalize-space(@ref) != ''">
						<xsl:value-of select="@ref"/>
					</xsl:when>
					<xsl:when test="normalize-space(@url) != ''">
						<xsl:value-of select="@url"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>#</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:copy-of select="@type"/>
			<xsl:apply-templates select="node()" mode="content"/>
		</a>
	</xsl:template>
	
	<xsl:template match="*" mode="root_escape" priority="0.1">
		<xsl:apply-templates select="*" mode="escape">
			<xsl:with-param name="root" select="1"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="@*" mode="escape">
		<xsl:text> </xsl:text>
		<xsl:value-of select="name(.)"/>
		<xsl:text>="</xsl:text>
		<xsl:value-of select="."/>
		<xsl:text>"</xsl:text>
	</xsl:template>
	
	<xsl:template match="*" mode="escape">
		<xsl:param name="root"/>
		<xsl:text><![CDATA[<]]></xsl:text>
		<xsl:value-of select="name(.)"/>
		<xsl:apply-templates select="@*" mode="escape"/>
		<xsl:for-each select="namespace::*[local-name(.) != 'xml']">
			<xsl:variable name="prefix" select="local-name(.)"/>
			<xsl:variable name="nsUri" select="."/>
			<xsl:if test="not(../../namespace::*[(local-name(.) = $prefix) and (. = $nsUri)]) or $root">
				<xsl:text> xmlns</xsl:text>
				<xsl:if test="$prefix != ''">:</xsl:if>
				<xsl:value-of select="$prefix"/>="<xsl:value-of select="$nsUri"/>
				<xsl:text>"</xsl:text>
			</xsl:if>
		</xsl:for-each>
		<xsl:choose>
			<xsl:when test="node()">
				<xsl:text><![CDATA[>]]></xsl:text>
				<xsl:apply-templates select="node()" mode="escape"/>
				<xsl:text><![CDATA[<]]>/</xsl:text>
				<xsl:value-of select="name(.)"/>
				<xsl:text><![CDATA[>]]></xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>/<![CDATA[>]]></xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
